package com.mz361.stdapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.mz361.common.utils.ResultVoUtil;
import com.mz361.common.vo.ResultVo;
import com.mz361.component.jwt.annotation.IgnorePermissions;
import com.mz361.stdapi.domain.*;
import com.mz361.stdapi.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author niceale
 * @ date 2024/04/17
 */
@Api(tags = "对外开放接口")
@RestController
@Slf4j
public class OPENAPIController {

    @Autowired
    private STDAPIService stdapiService;
    @Autowired
    private BasicSearchService basicSearchAPIService;
    @Autowired
    private StandardProfileService standardProfileService;
    @Autowired
    private StandardDynamicService standardDynamicService;
    @Autowired
    private StandardNoticeService standardNoticeService;
    @Autowired
    private StandardPlanService standardPlanService;

    @ApiOperation(value = "获取分类信息", notes = "获取分类信息")
    @IgnorePermissions
    @CrossOrigin
    @GetMapping("/api/v2/standardClass")
    public ResultVo standardClass() {
        return ResultVoUtil.success("请求成功", stdapiService.getStandardClass());
    }


    @ApiOperation(value = "基础搜索", notes = "通过关键字等搜索标准信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "keyword", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "检索词", required = true),
            @ApiImplicitParam(name = "status", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "有效性状态码"),
            @ApiImplicitParam(name = "standardClass", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "父分类编码"),
            @ApiImplicitParam(name = "classCode", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "所属子分类编码"),
            @ApiImplicitParam(name = "className", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "所属子分类名称"),
            @ApiImplicitParam(name = "havePdf", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "是否有电子文本"),
            @ApiImplicitParam(name = "modiDate", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "发布年份"),
            @ApiImplicitParam(name = "pageNo", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "当前页数，不传默认是1"),
            @ApiImplicitParam(name = "pageSize", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "每页条数，不传默认是15")
    })
    @CrossOrigin
    @GetMapping("/api/v2/basicSearch")
    public ResultVo v2basicSearch(BasicSearch basicSearch, @RequestHeader String key) {
        log.info("v2basicSearch  basicSearch: {} ", JSONObject.toJSONString(basicSearch));
        BasicResult basicResult = basicSearchAPIService.v2basicSearch(key, basicSearch);
        if (null != basicResult) {
            return ResultVoUtil.success("请求成功", basicResult);
        } else {
            return ResultVoUtil.error("接口错误");
        }
    }

    @ApiOperation(value = "标准概要", notes = "查询标准的题录信息，非文本内容信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true)

    })
    @CrossOrigin
    @GetMapping("/api/v2/standardProfile")
    public ResultVo<StandardData> v2standardProfile(String standardNo, @RequestHeader String key) {
        log.info("v2standardProfile standardNo:{}", standardNo);
        return ResultVoUtil.success("请求成功", standardProfileService.standardProfile(key, standardNo));
    }

    @ApiOperation(value = "标准文件地址获取", notes = "获取标准文件的地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true)

    })
    @CrossOrigin
    @GetMapping("/api/v2/fileUrl")
    public ResultVo v2fileUrl(String standardNo, @RequestHeader String key) {
        log.info("v2fileUrl standardNo:{}", standardNo);
        return ResultVoUtil.success("请求成功", stdapiService.v2fileURL(key, standardNo));
    }

    @ApiOperation(value = "标准文件预览图片地址获取（最多10页）", notes = "标准文件预览图片地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true),
            @ApiImplicitParam(name = "start", dataType = "Integer", dataTypeClass = Integer.class, paramType = "query", value = "起始图片位置", required = true),
            @ApiImplicitParam(name = "end", dataType = "Integer", dataTypeClass = Integer.class, paramType = "query", value = "结束图片位置", required = true)

    })
    @CrossOrigin
    @GetMapping("/api/v2/imageUrls")
    public ResultVo v2imageUrls(String standardNo, Integer start, Integer end) {
        log.info("imageUrls  standardNo:{}", standardNo);
        return ResultVoUtil.success("请求成功", stdapiService.v2imageURLs(standardNo, start, end));
    }

    @ApiOperation(value = "下载回调", notes = "下载完成后回调的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true),
            @ApiImplicitParam(name = "title", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准名称", required = true)

    })
    @CrossOrigin
    @GetMapping("/api/v2/downLoadNotify")
    public ResultVo v2downLoadNotify(String standardNo, String title, @RequestHeader String key) {
        log.info("v2downLoadNotify standardNo:{} title:{}", standardNo, title);
        boolean downloadStatus = stdapiService.v2downLoadNotify(key, standardNo, title);
        return ResultVoUtil.success("回调通知成功", downloadStatus);
    }

    @ApiOperation(value = "在线阅读回调", notes = "在线阅读回调接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true),
            @ApiImplicitParam(name = "title", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true)

    })
    @CrossOrigin
    @GetMapping("/api/v2/readOnlineNotify")
    public ResultVo v2readOnlineNotify(String standardNo, String title, @RequestHeader String key) {
        log.info("v2readOnlineNotify standardNo:{} title:{}", standardNo, title);
        boolean readOnlineStatus = stdapiService.v2readOnLineNotify(key, standardNo, title);
        return ResultVoUtil.success("回调通知成功", readOnlineStatus);
    }

    @ApiOperation(value = "标准计划", notes = "标准最近发布计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "keyword", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "关键字"),
            @ApiImplicitParam(name = "ccs", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "分类"),
            @ApiImplicitParam(name = "progress", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "项目进度 起草、征求意见、审查、批准、终止"),
            @ApiImplicitParam(name = "durDate", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "时间段 0:近一月,1:近三月,2:近半年,3:近两年,4:近三年"),
            @ApiImplicitParam(name = "pageNo", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "当前页数，不传默认是0"),
            @ApiImplicitParam(name = "pageSize", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "每页条数，不传默认是8")

    })
    @CrossOrigin
    @IgnorePermissions
    @GetMapping("/api/v2/standardPlans")
    public ResultVo<Page<StandardPlan>> v2standardPlans(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, PlanSearch planSearch) {
        log.info("v2standardPlans key:{} timestamp:{} token:{} planSearch:{} ", key, timestamp, token, planSearch);

        Page<StandardPlan> plans = standardPlanService.getPageList(planSearch);
        return ResultVoUtil.success("请求成功", plans);
    }

    @ApiOperation(value = "标准计划详情", notes = "标准计划详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardPlanNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "标准编号", required = true)

    })
    @IgnorePermissions
    @CrossOrigin
    @GetMapping("/api/v2/standardPlanInfo")
    public ResultVo<StandardPlan> v2standardPlanInfo(String standardPlanNo, @RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token) {
        log.info("v2standardPlanInfo key:{} timestamp:{} token:{} standardPlanNo:{}", key, timestamp, token, standardPlanNo);
        return ResultVoUtil.success("请求成功", standardPlanService.getByStandardPlanNo(standardPlanNo));
    }

    @ApiOperation(value = "标准公告", notes = "最新标准公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "noticeType", defaultValue = "0", dataType = "String", dataTypeClass = Number.class, paramType = "query", value = "公告类型，国家标准公告:0(默认值),行业标准公告:8,地方标准公告:9"),
            @ApiImplicitParam(name = "pageNo", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "当前页数，不传默认是0"),
            @ApiImplicitParam(name = "pageSize", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "每页条数，不传默认是8")

    })
    @CrossOrigin
    @IgnorePermissions
    @GetMapping("/api/v2/standardNotices")
    public ResultVo<Page<StandardNoticeObj>> v2standardNotices(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, @RequestHeader String userId, Integer pageNo, Integer pageSize, String noticeType) {
        log.info("v2standardNotices key:{} timestamp:{} token:{} userId:{} pageNo:{} pageSize:{} noticeType:{}", key, timestamp, token, userId, pageNo, pageSize, noticeType);
        return ResultVoUtil.success("请求成功", standardNoticeService.getPageList(noticeType, "", pageNo, pageSize));
    }

    @ApiOperation(value = "标准公告详情", notes = "标准公告详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "standardNoticeNo", dataType = "String", dataTypeClass = String.class, paramType = "query", value = "计划号", required = true)

    })
    @CrossOrigin
    @IgnorePermissions
    @GetMapping("/api/v2/standardNoticeInfo")
    public ResultVo<StandardNotice> v2standardNoticeInfo(String standardNoticeNo) {
        log.info("v2standardNoticeInfo standardNoticeNo:{}", standardNoticeNo);
        return ResultVoUtil.success("请求成功", standardNoticeService.getByStandardNoticeNo(standardNoticeNo));
    }

    @ApiOperation(value = "标准动态", notes = "最新标准动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true),
            @ApiImplicitParam(name = "dynamicStatus", defaultValue = "0", dataType = "String", dataTypeClass = Number.class, paramType = "query", value = "动态类型 0：最新上线（默认），1：即将实施，2：最新被替代，3：最新废止"),
            @ApiImplicitParam(name = "pageNo", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "当前页数，不传默认是0"),
            @ApiImplicitParam(name = "pageSize", dataType = "Number", dataTypeClass = Number.class, paramType = "query", value = "每页条数，不传默认是8")

    })
    @CrossOrigin
    @IgnorePermissions
    @GetMapping("/api/v2/standardDynamics")
    public ResultVo<Page<StandardDynamic>> v2standardDynamics(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token, Integer pageNo, Integer pageSize, String dynamicStatus) {
        log.info("v2standardDynamics key:{} timestamp:{} token:{}  pageNo:{} pageSize:{}", key, timestamp, token, pageNo, pageSize);
        return ResultVoUtil.success("请求成功", standardDynamicService.getPageList(dynamicStatus, "", pageNo, pageSize));
    }

    @ApiOperation(value = "用户套餐信息", notes = "查询已购买套餐信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "渠道KEY", required = true),
            @ApiImplicitParam(name = "timestamp", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "时间戳", required = true),
            @ApiImplicitParam(name = "token", dataType = "String", dataTypeClass = String.class, paramType = "Header", value = "MD5(KEY+timestamp+密钥)", required = true)

    })
    @CrossOrigin
    @GetMapping("/api/v2/userPackage")
    public ResultVo<UserPackageResult> v2userPackage(@RequestHeader String key, @RequestHeader String timestamp, @RequestHeader String token) {
        log.info("v2userPackage key:{} timestamp:{} token:{}", key, timestamp, token);
        UserPackageResult userPackageResult = stdapiService.v2getUserPacakge(key);
        return ResultVoUtil.success("请求成功", userPackageResult);
    }


}
